import actionType from './actionType'

import {getPosts} from '../services/Posts'

// 开始请求
const startFetchBlogList = () => {

    return {
        type: actionType.START_FETCH_BLOG_LIST
    }
}

const fetchBlogListSuccess = (payload) => {
    return {
        type: actionType.FETCH_BLOG_LIST_SUCCESS,
        payload
    }
}

const fetchBlogListFaild = () => {
    return {
        type: actionType.FETCH_BLOG_LIST_FAILED
    }
}

export const fetchBlogList = () => dispatch => {
    dispatch(startFetchBlogList())
    getPosts()
        .then(resp => {
            console.log(resp)
            // 用!==来测试异常
            if(resp.status === 200){
                dispatch(fetchBlogListSuccess({
                    list: resp.data
                }))
            }else {
                dispatch(fetchBlogListFaild())
            }
        })
        .catch(error => {
            console.log(error)
            dispatch(fetchBlogListFaild())
        })
}